<?php
$arr_menu = array();
$generate_menu = '<ul class="clearfix round_all">';
$common_menu_icon_path = $menu_icon_path = Yii::app()->params['backendUrl'].'/images/icons/small/grey';
$current_menu_icon_path = Yii::app()->params['backendUrl'].'/images/icons/small/white';
$current_menu_link = 'index.php?r='.Yii::app()->getController()->getRoute();

if(Yii::app()->user->administrator_group_type == 'administrator')
{
    $sql = "SELECT * FROM site_administrator_menu, site_administrator_privilege
            WHERE administrator_menu_id = administrator_privilege_administrator_menu_id
            AND administrator_menu_is_active = '1'
            AND administrator_privilege_administrator_group_id = '".Yii::app()->user->administrator_group_id."'";
}
else
{
    $sql = "SELECT * FROM site_administrator_menu
            WHERE administrator_menu_is_active = '1'";
}

$rs = Yii::app()->db->createCommand($sql)->queryAll();

if($rs != null)
{
  foreach($rs as $row)
  {
    $arr_menu[$row['administrator_menu_par_id']][$row['administrator_menu_order_by']] = $row;
  }

  // cari root menu
  if(array_key_exists('0', $arr_menu))
  {
    // urutkan root menu berdasarkan menu_order_by
    ksort($arr_menu[0]);

    // ekstrak root menu
    foreach($arr_menu[0] as $rootmenu_sort => $rootmenu_value)
    {
      // set menu icon
      $rootmenu_icon = '';
      if($rootmenu_value['administrator_menu_icon'] != '')
      {
        $rootmenu_icon = '<img src="'.$menu_icon_path.'/'.$rootmenu_value['administrator_menu_icon'].'" />';
      }

      // cari submenu 1
      if(array_key_exists($rootmenu_value['administrator_menu_id'], $arr_menu))
      {
        $generate_menu .= '<li><a href="#">'.$rootmenu_icon.$rootmenu_value['administrator_menu_title'].'</a>';
        $generate_menu .= '<ul class="dropdown">';

        // urutkan submenu 1 berdasarkan menu_order_by
        ksort($arr_menu[$rootmenu_value['administrator_menu_id']]);
        
        // ekstrak submenu 1 yang par_id adalah menu_id dari root menu
        foreach($arr_menu[$rootmenu_value['administrator_menu_id']] as $submenu_1_sort => $submenu_1_value)
        {
          // set menu icon
          $submenu_1_icon = '';
          if($submenu_1_value['administrator_menu_icon'] != '')
          {
            $submenu_1_icon = '<img src="'.$menu_icon_path.'/'.$submenu_1_value['administrator_menu_icon'].'" />';
          }

          // cari submenu 2
          if(array_key_exists($submenu_1_value['administrator_menu_id'], $arr_menu))
          {
            $generate_menu .= '<li><a class="has_slide" href="#">'.$submenu_1_value['administrator_menu_title'].'</a>';
            $generate_menu .= '<ul class="slideout">';

            // urutkan submenu 2 berdasarkan menu_order_by
            ksort($arr_menu[$submenu_1_value['administrator_menu_id']]);

            // ekstrak submenu 2 yang par_id adalah menu_id dari submenu 1
            foreach($arr_menu[$submenu_1_value['administrator_menu_id']] as $submenu_2_sort => $submenu_2_value)
            {
              $generate_menu .= '<li><a href="'.Yii::app()->baseUrl.$submenu_2_value['administrator_menu_link'].'">'.$submenu_2_value['administrator_menu_title'].'</a></li>';
            }

            $generate_menu .= '</ul>';
          }
          else
          {
            $generate_menu .= '<li><a href="'.Yii::app()->baseUrl.$submenu_1_value['administrator_menu_link'].'">'.$submenu_1_icon.$submenu_1_value['administrator_menu_title'].'</a></li>';
          }
        }
        $generate_menu .= '</ul>';
      }
      else
      {
        $generate_menu .= '<li><a href="'.Yii::app()->baseUrl.$rootmenu_value['administrator_menu_link'].'">'.$rootmenu_icon.$rootmenu_value['administrator_menu_title'].'</a></li>';
      }
    }
  }
}
else
{
  $generate_menu .= '<li><a href="#"><img src="'.$menu_icon_path.'/home.png"/>Dashboard</a></li>';
  $generate_menu .= '<li><a href="#"><img src="'.$menu_icon_path.'/locked_2.png"/>Logout</a></li>';
}
$generate_menu .= '</ul>';
?>

<div id="nav_top" class="clearfix round_top">
  <?php echo $generate_menu; ?>
</div>
<!-- #nav_top -->